﻿@using System.Data;
@using GatewayServer.Common;

@{
    ViewBag.PageTitle = "洪峰智能";

    Layout = "~/Views/Shared/PageTemplate.cshtml";
}

<script>
    $(document).ready(function () {
        //  $("#sel_SearchType option[value='@(ViewBag.Type)']").attr("selected", true);
        if ("@(ViewBag.order)" == "upd") {
            getEdit("@(ViewBag.l_guid)");
        }
        getConditionList_main();
        getConditionList_follow();
        getHasVariableDevice();
        getHasVariableDevice2();
    });

    function getEdit(lguid) {
        $.post("getLinkage", { "flag": Math.random(), guid: lguid }, function (result) {
            var myobj = (result.data);
            $("#l_title").val(myobj.l_title);
           
            for (var i = 0; i < document.getElementById("l_status").options.length; i++) {
                if (document.getElementById("l_status").options[i].value == myobj.l_status) {
                    document.getElementById("l_status").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("l_status").options[i].selected = false;

                }
            }

             for (var i = 0; i < document.getElementById("l_condition_type").options.length; i++) {
                if (document.getElementById("l_condition_type").options[i].value == myobj.l_condition_type) {
                    document.getElementById("l_condition_type").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("l_condition_type").options[i].selected = false;

                }
            }
        });


        // $.post("getWarn", { "flag": Math.random(), guid: lguid }, function (result) {
        //     if(result.data==null)
        //     {
        //         return;
        //     }
        //     var myobj = (result.data);
        //     for (var i = 0; i < document.getElementById("w_status").options.length; i++) {
        //         if (document.getElementById("w_status").options[i].value == myobj.w_status) {
        //             document.getElementById("w_status").options[i].selected = true;
        //             break;
        //         }
        //         else {
        //             document.getElementById("w_status").options[i].selected = false;
        //         }
        //     }
        // });
    }

    function getHasVariableDevice() {
        $("#seldevice").empty();
        $("#seldevice").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getHasVariableDevice", { "flag": Math.random(), export: 0, p_guid: $("#l_p_guid").val() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#seldevice").prepend("<option value='" + myobj[i].d_guid + "'>" + myobj[i].d_name + "</option>")
            }
        });
    }
    function getHasVariableDevice2() {
        $("#seldevice2").empty();
        $("#seldevice2").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getHasVariableDevice", { "flag": Math.random(), export: 1, p_guid: $("#l_p_guid").val() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#seldevice2").prepend("<option value='" + myobj[i].d_guid + "'>" + myobj[i].d_name + "</option>")
            }
        });
    }
    function getVariableByDevice(dguid) {
        $("#selvar").empty();
        $("#selvar").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getVariableByDevice", { "flag": Math.random(), guid: dguid, export: 0 }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#selvar").prepend("<option value='" + myobj[i].v_guid + "'>" + myobj[i].v_name + "</option>")
            }
        });
    }

    function isDelay(dguid) {
        $.post("getDeviceModel", { "flag": Math.random(), guid: dguid }, function (result) {
            var myobj = (result.data);
            //延时
            $("#lc_value").val("");
            if (myobj.m_code == "1017") {
                //延时
               
                document.getElementById("divlcvalue").style.display = "";
            }
            else {
                document.getElementById("divlcvalue").style.display = "none";
            }



        });
    }

    function getCommandByDevice(dguid) {
        $("#lc_dc_guid").empty();
        $("#lc_dc_guid").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getCommandList", { "flag": Math.random(), guid: dguid }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#lc_dc_guid").prepend("<option value='" + myobj[i].dc_guid + "'>" + myobj[i].dc_name + "</option>")
            }
        });
    }
    //function getVariableByDevice2(dguid) {
    //    $("#selvar2").empty();
    //    $("#selvar2").append("<option value=''  selected='selected'>请选择</option>");
    //    $.post("getVariableByDevice", { "flag": Math.random(), guid: dguid, export: 1 }, function (result) {
    //        var myobj = (result.data);
    //        for (var i = myobj.length - 1; i >= 0; i--) {
    //            $("#selvar2").prepend("<option value='" + myobj[i].v_guid + "'>" + myobj[i].v_name + "</option>")
    //        }
    //    });
    //}
    function getVariableValueRlue(vguid) {
        $("#selvarvalue").empty();
        $("#selvarvalue").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getVariableValueRlue", { "flag": Math.random(), guid: vguid, export: 0 }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#selvarvalue").prepend("<option value='" + myobj[i].vr_guid + "'>" + myobj[i].vr_name + "</option>")
            }
        });
    }


    function getVariable(guid) {
     
        $.post("getVariable", { "flag": Math.random(), guid: guid }, function (result) {
            var myobj = (result.data);
            $("#conditionValue").val("");
           if(myobj.dv_export=="1")
           {
               //隐藏div
                document.getElementById("divVisible").style.display="none";
           }
           else
           {
                //显示div
                document.getElementById("divVisible").style.display = "";
           }
        });
    }


    function saveTemp(type) {
        if($("#l_title").val()=="")
        {
            alert("请填写场景标题");
            return false;
        }
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("saveTemp?type=" + type, data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                getConditionList_main();
                getConditionList_follow();
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }

    function getConditionList_main() {
        $.post("getConditionListTemp", { "flag": Math.random(), guid: $("#l_guid").val(), type: "main" }, function (result) {
            var pagedata = (result.data);
            rowcount = 0;
            var htmlstr = "";
            htmlstr = htmlstr + "<tr class='plainItemTRTitle'>";
            htmlstr = htmlstr + "<td >序号</td >";
            htmlstr = htmlstr + "<td >设备</td >";
            htmlstr = htmlstr + "<td >变量</td >";
            htmlstr = htmlstr + "<td >触发条件</td >";
            htmlstr = htmlstr + "<td >阀值</td >";
            htmlstr = htmlstr + "<td >操作</td >";
            htmlstr = htmlstr + "</tr >";
            document.getElementById("pagelist_main").innerHTML = "";
            if (pagedata != null && pagedata.length > 0) {
                for (var i = 0; i < pagedata.length; i++) {
                    rowcount = i + 1;
                    htmlstr = htmlstr + "<tr>";
                    htmlstr = htmlstr + "<td >" + rowcount + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].name + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].dvname + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].vrname + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].value + "</td >";
                    htmlstr = htmlstr + "<td ><a href=\"javascript:delTemp('" + pagedata[i].lc_guid + "')\">删除</a> </td >";
                    htmlstr = htmlstr + "</tr >";

                }
                document.getElementById("pagelist_main").innerHTML = htmlstr;
            }
        });
    }

    function getConditionList_follow() {
        $.post("getConditionListTemp", { "flag": Math.random(), guid: $("#l_guid").val(), type: "follow" }, function (result) {
            var pagedata = (result.data);
            rowcount = 0;
            var htmlstr = "";
            htmlstr = htmlstr + "<tr class='plainItemTRTitle'>";
            htmlstr = htmlstr + "<td >序号</td >";
            htmlstr = htmlstr + "<td >设备</td >";
            htmlstr = htmlstr + "<td >指令</td >";
            htmlstr = htmlstr + "<td >值</td >";
            htmlstr = htmlstr + "<td >次</td >";
            htmlstr = htmlstr + "<td >间隔-秒</td >";
            htmlstr = htmlstr + "<td >操作</td >";
            htmlstr = htmlstr + "</tr >";
            document.getElementById("pagelist_follow").innerHTML = "";
            if (pagedata != null && pagedata.length > 0) {
                for (var i = 0; i < pagedata.length; i++) {
                    rowcount = i + 1;
                    htmlstr = htmlstr + "<tr>";
                    htmlstr = htmlstr + "<td >" + rowcount + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].name + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].dcname + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].value + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].lc_repeat_count + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].lc_repeat_interval + "</td >";
                    htmlstr = htmlstr + "<td ><a href=\"javascript:delTemp('" + pagedata[i].lc_guid + "')\">删除</a> </td >";
                    htmlstr = htmlstr + "</tr >";

                }
                document.getElementById("pagelist_follow").innerHTML = htmlstr;
            }
        });
    }

    function delTemp(guid) {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("delTemp?guid=" + guid, data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                getConditionList_main();
                getConditionList_follow();
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }

    function savelinkage() {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("savelinkage", data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
             //   window.location.href = "list";
                document.getElementById("linkagelable").innerHTML = "保存成功!";
            // $("#linkagelable").innerHTML="保存成功!";
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }
</script>

<style>
    #pagelist_follow {
        width: 100%;
    }

    #pagelist_main {
        width: 100%;
    }
    .kv-item label{width:100px;}

    .plainItemTable td {
        line-height: 30px;
    }

    .plainItemTRTitle td {
        font-weight: bold;
        background-color: aliceblue;
    }
    .divset{
       margin-top:10px;
       border-radius:5px;
       margin-left:5px;
        padding:10px;
       border:1px solid #96c2f1;background:#eff7ff;
        width: 48%;
        float: left;
        
    }
</style>
<div id="saper-container">
    <form id="addForm" name="addForm" action="list" method="post">
        <input type="hidden" name="l_p_guid" id="l_p_guid" value="@ViewBag.l_p_guid" />
       
        <div id="saper-hd"></div>
        <div id="saper-bd">
            <div class="subfiled clearfix">
                <h2>条件触发场景</h2>
            </div>
            <div class="subfiled-content">

                <div id="" title="添加/编辑">
                    <form action="" name="addForm" id="addForm" method="post">

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>场景标题：</label>
                            <div class="kv-item-content">
                                <input type="text" name="l_title" id="l_title" placeholder="场景标题" />
                                <input type="hidden" name="l_guid" id="l_guid" value="@ViewBag.l_guid" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>状态：</label>
                            <div class="kv-item-content">
                                <select id="l_status" name="l_status" style="width:130px;">
                                    <option value="0">启用</option>
                                    <option value="1">禁用</option>
                                </select>

                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>条件约束：</label>
                            <div class="kv-item-content">
                                <select id="l_condition_type" name="l_condition_type" style="width:130px;">
                                    <option value="and">满足全部条件时触发</option>
                                    <option value="or">满足任一条件时触发</option>
                                </select>
                            </div>
                        </div>
                       @*  <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>触发预警：</label>
                            <div class="kv-item-content">
                                <select id="w_status" name="w_status" style="width:130px;">
                                    <option value="0">关闭</option>
                                    <option value="1">预警</option>
                                </select>

                            </div>
                        </div> *@
                        <div class="kv-item clearfix">
                            <label></label>
                            <div class="kv-item-content">
                                <input type="button" id="save" name="save" value="保存" onclick="javascript:savelinkage()" />
                                <span id="linkagelable"></span>
                            </div>
                        </div>

                        
                        <div><hr /></div>
                        <div style="background-color:antiquewhite;line-height:25px;padding:5px;font-weight: bold;">触发器</div>
                        <div style="" class="divset">
                            <div>配置条件</div>  
                            <div><hr /></div>
                            <div class="kv-item clearfix">
                                <label>我的设备：</label>
                                <div class="kv-item-content">
                                    <select id="seldevice" name="seldevice" style="width:130px;" onchange="getVariableByDevice(this.value)">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="kv-item clearfix">
                                <label>设备变量：</label>
                                <div class="kv-item-content">
                                    <select id="selvar" name="selvar" style="width:130px;" onchange="getVariableValueRlue(this.value);getVariable(this.value);">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div id="divVisible" style="display:none">
                            <div class="kv-item clearfix">
                                <label>触发条件：</label>
                                <div class="kv-item-content">
                                    <select id="selvarvalue" name="selvarvalue" style="width:130px;">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="kv-item clearfix">
                                <label>条件阀值：</label>
                                <div class="kv-item-content">
                                    <input type="text" id="conditionValue" name="conditionValue" value="" style="width:110px;" />
                                    (数值/open/close/yes/no)
                                </div>
                            </div>
                            </div>
                            <div class="kv-item clearfix">
                                <div class="kv-item-content" style="margin-left:100px;">
                                    <input type="button" id="save" name="save" value="添加条件" onclick="javascript:saveTemp('main')" />
                                </div>
                            </div>

                            <div>
                                <table id="pagelist_main" class="plainItemTable"></table>
                            </div>

                        </div>
                        <div style="" class="divset">
                            <div>配置触发</div>
                            <div><hr /></div>
                            <div class="kv-item clearfix">
                                <label>我的设备：</label>
                                <div class="kv-item-content">
                                    <select id="seldevice2" name="seldevice2" style="width:130px;" onchange="getCommandByDevice(this.value);isDelay(this.value);">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="kv-item clearfix">
                                <label>触发执行：</label>
                                <div class="kv-item-content">
                                    <select id="lc_dc_guid" name="lc_dc_guid" style="width:130px;">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>

                             <div class="kv-item clearfix">
                                <label>执行次数：</label>
                                <div class="kv-item-content">
                                    <input type="text" id="lc_repeat_count" name="lc_repeat_count" value="1" style="width:110px;" /> -1无限次，或1-N次，-100停止无限
                                </div>
                            </div>
                            <div class="kv-item clearfix">
                                <label>执行次数间隔：</label>
                                <div class="kv-item-content">
                                    <input type="text" id="lc_repeat_interval" name="lc_repeat_interval" value="10" style="width:110px;" />秒，如次数为1或-100次，这里无意义
                                </div>
                            </div>
                            <div class="kv-item clearfix" style="display:none" id="divlcvalue" name="divlcvalue">
                                <label>值：</label>
                                <div class="kv-item-content">
                                    <input type="text" id="lc_value" name="lc_value" value="" style="width:110px;" />秒
                                </div>
                            </div>
                            <div class="kv-item clearfix">
                                <div class="kv-item-content" style="margin-left:100px;">
                                    <input type="button" id="save" name="save" value="添加触发内容" onclick="javascript:saveTemp('follow')" />
                                </div>
                            </div>

                            <div>
                                <table id="pagelist_follow" class="plainItemTable"></table>
                            </div>
                        </div>
                        <div style="clear:both"></div>
                        <div><hr /></div>
                        <div style="margin:0 auto;width:150px;">
                            &nbsp;&nbsp; <input type="button" id="but1" name="but1" value="返回列表" onclick="javascript:window.history.back()" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div id="saper-ft"></div>
    </form>
</div>

